/*-------------------------------------------------------------------------
 *
 * pg_config.c
 *		曝光与pg_config相同的输出，除了作为SRF
 *
 * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
 * Portions Copyright (c) 1994, Regents of the University of California
 *
 * IDENTIFICATION
 *	  src/backend/utils/misc/pg_config.c
 *
 *-------------------------------------------------------------------------
 */

#include "postgres.h"

#include "catalog/pg_type.h"
#include "common/config_info.h"
#include "funcapi.h"
#include "miscadmin.h"
#include "port.h"
#include "utils/builtins.h"

Datum pg_config(PG_FUNCTION_ARGS)
{
	ReturnSetInfo *fc_rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
	ConfigData *fc_configdata;
	size_t		fc_configdata_len;
	int			fc_i = 0;

	/* 初始化我们的元组存储 */
	InitMaterializedSRF(fcinfo, 0);

	fc_configdata = get_configdata(my_exec_path, &fc_configdata_len);
	for (fc_i = 0; fc_i < fc_configdata_len; fc_i++)
	{
		Datum		fc_values[2];
		bool		fc_nulls[2];

		memset(fc_values, 0, sizeof(fc_values));
		memset(fc_nulls, 0, sizeof(fc_nulls));

		fc_values[0] = CStringGetTextDatum(fc_configdata[fc_i].name);
		fc_values[1] = CStringGetTextDatum(fc_configdata[fc_i].setting);

		tuplestore_putvalues(fc_rsinfo->setResult, fc_rsinfo->setDesc, fc_values, fc_nulls);
	}

	return (Datum) 0;
}
